//
//  AddSizeViewController.m
//  quickBatchManager
//
//  Created by 王定波 on 15/9/22.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "AddSizeViewController.h"

@interface AddSizeViewController ()
{
    NSMutableArray *buttonList;
}
@end

@implementation AddSizeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)initUI
{
    UIImage *image = _bgImage.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
    _bgImage.image=image;
    self.view.backgroundColor=[UIColor clearColor];
    [_addSizeBtn bk_whenTapped:^{
        if(![_sizeField.text isBlank])
        {
            SizeModel *size=[[SizeModel alloc] init];
            size.sizeName=_sizeField.text;
            [SizeModel insertToDB:size];
            [_sizeField resignFirstResponder];
            _sizeField.text=@"";
            [self reloadData];
        }
        else [self showAlert:@"请输入"];
    }];
    
    [_submitBtn bk_whenTapped:^{
        NSMutableArray *selectList=[[NSMutableArray alloc] init];
        for (UIButton *button in buttonList) {
            if(button.selected)
            {
                [selectList addObject:button.titleLabel.text];
            }
            
        }
        if(selectList.count==0)
        {
            [self showAlert:@"未选择"];
        }
        else {
         
            if(_isRadios)
            {
                if([_delegate respondsToSelector:@selector(didSelectData:andData:)])
                {
                    [_delegate didSelectData:ChooseTypeSize andData:selectList[0]];
                }
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            else
            {
                if([_delegate respondsToSelector:@selector(didSelectDatas:andData:)])
                {
                    [_delegate didSelectDatas:ChooseTypeSize andData:selectList];
                }
                [self dismissViewControllerAnimated:YES completion:nil];
            }
          
        }
    }];
    
    
    [_closeBtn bk_whenTapped:^{
           [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self reloadData];
    
}


-(void) initData
{
    buttonList=[[NSMutableArray alloc] init];
}

-(void) reloadData
{
    for (UIView *view in _listScrollView.subviews) {
        [view removeFromSuperview];
    }
    [buttonList removeAllObjects];
    
    int scrollwidth=405;
    
    NSArray *listsize=[SizeModel searchWithWhere:nil];
    int i=0;
    for (SizeModel *size in listsize) {
        UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake((scrollwidth/3)*(i%3)+10, (i/3)*45, scrollwidth/3-20, 35)];
        [button setTitle:size.sizeName forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:0.68 green:0.40 blue:0.22 alpha:1] forState:UIControlStateNormal];
        [button setBackgroundImage:[ToolUtils imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[ToolUtils imageWithColor:[UIColor colorWithRed:1 green:0.84 blue:0.66 alpha:1]] forState:UIControlStateSelected];
        [button setBackgroundImage:[ToolUtils imageWithColor:[UIColor colorWithWhite:0.8 alpha:1]] forState:UIControlStateDisabled];
        
        button.layer.cornerRadius=5;
        button.clipsToBounds=YES;
        button.titleLabel.font=[UIFont systemFontOfSize:15];
        
        for (NSString *str in _selectList) {
            if([str isEqualToString:size.sizeName])
            {
                [button setEnabled:NO];
                break;
            }
        }
        
        
        [button bk_whenTapped:^{
            if(_isRadios)
            {
                for (UIButton *sbutton in buttonList) {
                   if(![sbutton isEqual:button])
                       [sbutton setSelected:NO];
                    else [button setSelected:YES];
                }
            }
            else  button.selected=(!button.selected);
        }];
        [_listScrollView addSubview:button];
        [buttonList addObject:button];
        i++;
    }
    
    _listScrollView.contentSize=CGSizeMake(scrollwidth, (i/3+1)*45);
    
}
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    [super touchesBegan:touches withEvent:event];
//    [self dismissViewControllerAnimated:YES completion:nil];
//}

@end
